package com.jiuli_travel.user_service.controller;


import com.jiuli_travel.user_service.model.cache.UserCache;
import com.jiuli_travel.user_service.model.dto.user.UserInsertRequest;
import com.jiuli_travel.user_service.service.IUserService;
import com.jiuli_travel.utils.Result;
import com.jiuli_travel.utils.UserHolder;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     *  注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserInsertRequest userInsertRequest){
        return userService
                .register(
                        userInsertRequest.getUserAccount(),
                        userInsertRequest.getPassword(),
                        userInsertRequest.getRePassword()
                );
    }


    /**
     *  登录
     */
    @GetMapping("/login")
    public Result login(String userAccount, String userPassword){
        return userService.login(userAccount, userPassword);
    }


    /**
     *  获取当前登录用户的信息
     */
    @GetMapping("/getLoginUserInfo")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    public Result getLoginUserInfo(){
        return Result.ok(UserHolder.getUser());
    }


    /**
     *  根据 user_id 获取用户的信息
     */
    @GetMapping("/getUserByUserId")
    public Result getUserByUserId(Long userId){
        return userService.getUserByUserId(userId);
    }

    /**
     * 用户信息的编辑 （也是很牛的一个东西） 实现接口
     */


    /**
     * 退出登录
     */
    @PutMapping("/logout")
    public void logout(){
        userService.logout();
    }
}



















